//外接订单

interface IOperationManagentViewModel extends IBaseViewModel<IOperationQueryTerms>{
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<carryOrderListResponse>;
    /**
     * 时间日期格式
     */
    dateFormat: "{format:'YYYY.MM.DD HH:mm'}";
    /**
     * 查看详情事件
     */
    orderAddEdit:()=>void;
    /**
     * 列表高度
     */
    heightA:number;
    /**
     * 车辆定位
     */
    carLocation:boolean;
    /**
     * 订单状态下拉
     */
    orderStatusDropDown:ValueListItemResponse[];
    /**
     * 客服专员下拉
     */
    customerServiceDropDown:EmployeeListItemResponse[];
    /**
     * 调度专员下拉
     */
    dispatchOfficerDropDown: EmployeeListItemResponse[];
     /**
     * 合同状态下拉
     */
    contractStatusDropDown:ValueListItemResponse[];
    /**
     * 回单状态下拉
     */
    receiptStatusDropDown:ValueListItemResponse[];
}

interface IOperationQueryTerms{
    /**
     * 订单编号
     */
    orderId:string;
    /**
     * 客户单位
     */
    client:string;
    /**
     * 线路地址
     */
    route:string;
    /**
     * 合同号
     */
    contractNum:string;
    /**
     * 发货地址
     */
    shipAddress:string;
    /**
     * 送货地址
     */
    endAddres:string;
    /**
     * 车牌号
     */
    carCode:string;
    /**
     * 回单状态
     */
    receiptPriceStatus:string;
    /**
     * 客服专员     
     */
    customerServiceOfficerId:string;
    /**
     * 调度专员
     */
    dispatchOfficerId:string;
    /**
     * 订单状态
     */
    orderStatus:string;
    /**
     * 合同状态 
     */
    contractStatus:string;
    /**
     * 发货起始时间
     */
    shipStartTime:string;
    /**
     * 发货结束时间
     */
    shipEndTime:string;
}


class IOperationController extends ListController<IOperationManagentViewModel,IOperationQueryTerms>{
    gridApi: uiGrid.IGridOptionsOf<carryOrderListResponse>;
    orderBy: number = 0;
    asc: boolean = true;

    /**
     * 单选
     */
    selectData:carryOrderListResponse;
    skip:number = 0;

    constructor($scope:IOperationManagentViewModel, private valueService: IValueService,private employeeService:IEmployeeService,
    private $ngBootbox: BootboxService,private $state: angular.ui.IStateService,private routeService:routeService,private operationService:IOperationService,
    private $location:ng.ILocationService){
        super($scope);
        this.init();
    }

    /**
     * 初始化
     */
    init():void{
        super.init();
        this.$scope.carLocation = true;

        this.$scope.queryTerms = {
           orderId:"",
           client:"",
           route:"",
           contractNum:"",
           shipAddress:"",
           endAddres:"",
           carCode:"",
           receiptPriceStatus:"",
           customerServiceOfficerId:"",
           dispatchOfficerId:"",
           orderStatus:"",
           contractStatus:"",
           shipStartTime:"",
           shipEndTime:""
        };

        this.$scope.gridOptions = {
            multiSelect:false,//单选
            enableRowSelection: true,
            enableRowHeaderSelection: false,
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            columnDefs: [
                { displayName: "订单编号", field: 'orderId', cellTemplate: '<div  ng-repeat="item in row.entity.orderList"class="LTL-list">{{item.orderId}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '8%' },
                { displayName: "客户单位", field: 'clientName', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" class="LTL-list">{{item.clientName}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '9%' },
                { displayName: "发货地址", field: 'startAddress', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" class="LTL-list">{{item.startAddress}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '9%' },
                { displayName: "中转地", field: 'viaAddress', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" class="LTL-list">{{item.viaAddress}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '10%' },
                { displayName: "送货地址", field: 'endAddress', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" class="LTL-list">{{item.endAddress}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '9%' },
                { displayName: "发货时间", field: 'shipTime', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" class="LTL-list">{{item.shipTime}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '7%' },
                { displayName: "订单状态", field: 'orderStatus', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" class="LTL-list">{{item.orderStatus}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: '回单状态', field: 'receiptStatus', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" class="LTL-list">{{item.receiptStatus}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: "客服专员", field: 'customerService', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" class="LTL-list">{{item.customerService}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, enableSorting: false },
                { displayName: "调度专员", field: 'dispatcher', cellTemplate: '<div  ng-repeat="item in row.entity.orderList" class="LTL-list">{{item.dispatcher}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, enableSorting: false },
                { displayName: '合同号', field: 'contractNum', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list">{{row.entity.contractNum}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: '承运商名称', field: 'carrierName', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list">{{row.entity.carrierName}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, enableSorting: false },
                { displayName: '承运车牌号', field: 'carryCarCode', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list">{{row.entity.carryCarCode}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: '合同状态', field: 'contractStatus', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list">{{row.entity.contractStatus}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: '油卡状态', field: 'oilCardStatus', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list">{{row.entity.oilCardStatus}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { 
                        displayName: '定位状态', field: 'locationStatus',
                        cellTemplate: '<div ng-if="row.entity.locationStatus ==true ">'
                                    + '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list">已开通</div>'
                                    + '</div>'
                                    + '<div ng-if="row.entity.locationStatus ==false ">'
                                        + '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list">未开通</div>'
                                        + '<div>'
                                        + '<div ng-if="row.entity.locationStatus == true" >'
                                        + '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list">已开通</div>'
                                        + '<div>',
                        enableColumnMenu: false, cellTooltip: true, headerTooltip: true, enableSorting: false
                    },
                { displayName: "承运车长", field: 'carLength', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list">{{row.entity.carryCarLength}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '应付单价', field: 'receivablePrice', cellTemplate: '<div style="height:{{39 * row.entity.orderList.length}}px;"class="single-list">{{row.entity.receivablePrice}}</div>', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, enableSorting: false },
        ],
        onRegisterApi: (gridApi) => {
            gridApi.pagination.on.paginationChanged(this.$scope,(skipSize,pageSize)=>{
                this.paginationChanged(skipSize,pageSize,this.$state);
            });
            // gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
            gridApi.core.on.sortChanged(this.$scope, this.sortChange);
            gridApi.selection.on.rowSelectionChanged(this.$scope, this.selectChange);
        }

        }
        $('#openStartTime').click(function () {
        $('#shipStartTime').datetimepicker('show');
        });
        $('#openEndTime').click(function () {
            $('#shipEndTime').datetimepicker('show');
        });


        this.query();
        this.$scope.orderAddEdit = this.showOrderDetail;
        this.loadDispatchOfficerDropDown();
        this.loadOrderStatusDropDown();
        this.loadCustomerServiceDropDown();
        this.loadContractStatusDropDown();
        this.loadReceiptStatusDropDown();

    }

    localHistory():void{
        super.localHistory(this.$state)
    }
    getHistory():void{
        super.getHistory(this.$state,this.$location)
    }
    

    /**
     * 加载数据
     */
    loadData(queryTerms:IOperationQueryTerms,skip:number,count:number):void{
        if (this.queryTerms.orderStatus == null || this.queryTerms.orderStatus == undefined || this.queryTerms.orderStatus == "") { this.queryTerms.orderStatus = "1,2,3,4,5,6,7,8" };
        
        this.operationService.getCarryOrderList(this.orderBy.toString(),this.asc,this.queryTerms.orderId,this.queryTerms.client,this.queryTerms.route,
        this.queryTerms.shipStartTime,this.queryTerms.shipEndTime,this.queryTerms.orderStatus,"","",this.queryTerms.shipAddress,this.queryTerms.endAddres,
        this.queryTerms.carCode,this.queryTerms.contractNum,this.queryTerms.contractStatus,this.queryTerms.receiptPriceStatus,"",this.queryTerms.customerServiceOfficerId,
        "",this.queryTerms.dispatchOfficerId,"","","",skip,count).then((result)=>{
            // this.$scope.gridOptions.totalItems = result.data.total;
                this.$scope.gridOptions.data = result.data.data;

                let gridHeight = 0;
                result.data.data.forEach((item,index)=>{
                    gridHeight = gridHeight + item.orderList.length;
                });
                this.$scope.heightA = (gridHeight * 39)+96;

                if (skip == 0) {
                    this.$scope.gridOptions.paginationCurrentPage = 1;
                }
                this.skip =skip;

                this.$scope.querying = false;
            }, error => {
                this.$scope.querying = false;
        });
    }

    
    getListCount():void{
        this.operationService.carrierOrderCount(this.orderBy.toString(),this.asc,this.$scope.queryTerms.orderId,this.$scope.queryTerms.client,this.$scope.queryTerms.route,
        this.$scope.queryTerms.shipStartTime,this.$scope.queryTerms.shipEndTime,this.$scope.queryTerms.orderStatus,"","",this.$scope.queryTerms.shipAddress,this.$scope.queryTerms.endAddres,
        this.$scope.queryTerms.carCode,this.$scope.queryTerms.contractNum,this.$scope.queryTerms.contractStatus,this.$scope.queryTerms.receiptPriceStatus,"",this.$scope.queryTerms.customerServiceOfficerId,
        "",this.$scope.queryTerms.dispatchOfficerId,"","","").then(res=>{
            this.$scope.gridOptions.totalItems = res.data.extData;
        })
    }


    /**
     * 调度专员下拉
     */
    loadDispatchOfficerDropDown():void{
        this.employeeService.getbytype("2",0,-1).then(result => {//CommissionerType.Dispatch.toString()
            this.$scope.dispatchOfficerDropDown = result.data.data;
        });
    }

    /**
     * 订单状态下拉
     */
    loadOrderStatusDropDown():void{
        let orderstatus = this.valueService.getOrderStatusList().data;
        orderstatus.splice(1,1);
        orderstatus.splice(6,1);
        this.$scope.orderStatusDropDown = orderstatus;
    }
    
    /**
     * 客服专员下拉
     */
    loadCustomerServiceDropDown():void{
        this.employeeService.getbytype("4",0,-1).then(result => {//CommissionerType.CustomerService.toString()
            this.$scope.customerServiceDropDown = result.data.data;
        });
    }

    /**
     * 合同状态下拉
     */
    loadContractStatusDropDown():void{
        this.$scope.contractStatusDropDown = this.valueService.getContractStatusList().data;
    }
    
    /**
     * 回单状态下拉
     */
    loadReceiptStatusDropDown():void{
        this.$scope.receiptStatusDropDown = this.valueService.getReceiptStatusList().data;
    }



    /**
     * 排序
     */
    sortChange=(grid:  uiGrid.IGridInstanceOf<carryOrderListResponse>, sortColumns: Array<uiGrid.IGridColumnOf<carryOrderListResponse>>)=>{
        if (sortColumns.length == 0) {
            this.reload();
        } else {
            let sortItem = sortColumns[0];
            switch (sortItem.name){
                case "orderId": {
                    this.orderBy = 1;
                } break;
                case "clientName": {
                    this.orderBy = 2;
                } break;
                case "startAddress": {
                    this.orderBy = 3;
                } break;
                case "endAddress": {
                    this.orderBy = 4;
                } break;
                case "shipTime": {
                    this.orderBy = 5;
                } break;
                case "orderStatus": {
                    this.orderBy = 6;
                } break;
                case "receiptStatus": {
                    this.orderBy = 7;
                } break;
                case "contractNum": {
                    this.orderBy = 8;
                } break;
                case "carCode": {
                    this.orderBy = 9;
                } break;
                case "carLength": {
                    this.orderBy = 10;
                } break;
                case "contractStatus": {
                    this.orderBy = 11;
                } break;
                case "oilCardStatus": {
                    this.orderBy = 12;
                } break;
                case "viaAddress":{
                    this.orderBy = 13;
                } break;
            }
            this.asc = sortItem.sort.direction == "asc";
            this.reload();
       }
    }

    /**
     * 单选
     */
    selectChange=(row:uiGrid.IGridRowOf<carryOrderListResponse>)=> {
        if (row.isSelected) {
            this.selectData = row.entity;
            this.$scope.carLocation = false
        } else {
            this.selectData = null;
            this.$scope.carLocation = true;
        }
    }

    /**
     * 查看详情
     */
    showOrderDetail=()=>{
        if(!this.selectData){
            this.$ngBootbox.alert("请选择一项");
            return;
        }
        if(this.selectData.orderList.length > 1 ){
            this.$state.go('app.opera.operationManage_breakBulk',{ id:this.selectData.id , type:this.selectData.orderList[0].orderStatus, skip:this.skip });
        }else{
            this.$state.go('app.opera.operation_detail',{ id:this.selectData.id ,  type:this.selectData.orderList[0].orderStatus, skip:this.skip });
        };
    }

}
angular.module("tms").controller("operationManagentController", ["$scope", "valueService","employeeService", "$ngBootbox",  "$state", "routeService","operationService","$location",IOperationController]);
   